package net.coldsky.common.util.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;

/**
 * 二进制文件串行化工具<p>
 * */
public abstract class FileUtils {

	/** ClassPath */
	public static final String CLASS_PATH="classpath:";
	
	public static void main(String[] args) {
		System.out.println(FileUtils.parseFile("classpath:/"));
		System.out.println(new File("c:/1.txt"));
		System.out.println(FileUtils.parseFile("c:/1.txt"));
	}
	
	//读写文件 
	
	/** 读文件 (路径) */
	public static byte[] readFromFile(String in){
		return readFromFile(FileUtils.parseFile(in));
	}
	
	/** 读文件 (具体的文件) */
	public static byte[] readFromFile(File in){
		byte[] b;
		try {
			b = FileCopyUtils.copyToByteArray(in);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return b;
	}
	
	/** 存文件 (具体的文件) */
	public static void saveToFile(byte[] in, File out){
		Assert.notNull(in);
		Assert.notNull(out);
		try {
			FileCopyUtils.copy(in, out);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	/** 存文件 (路径) */
	public static void saveToFile(byte[] in , String out){
		saveToFile(in, FileUtils.parseFile(out));
	}

	//操作文件(扩展) 
	
	/**
	 * 压缩并删除原始文件 压缩一个文件
	 *
	 * filePath:原来文件的路径
	 * zipFilePath:压缩后的路径
	 *
	 *
	 * @param path
	 * @param zipPath
	 * @param fileName
	 */
	public static void zipAndDelFile(String filePath, String zipFilePath){
		File file=new File(filePath);
		File zipFile=new File(zipFilePath);
		zipAndDelFile(file, zipFile);

	}

	public static void zipAndDelFile(File file, File zipFile) {
		Assert.notNull(file);
		Assert.notNull(zipFile);
		
		Assert.isTrue(file.exists() && file.canRead());
		
		FileInputStream fis=null;
		ZipOutputStream zip=null;
		byte[] b=new byte[1024*5];
		try {
			fis= new FileInputStream(file);
			zip=new ZipOutputStream(new FileOutputStream(zipFile));
			zip.putNextEntry(new ZipEntry(file.getName()));
			zip.setLevel(9);
			int readNum=-1;
			while((readNum=fis.read(b))!=-1){
				zip.write(b,0,readNum);
			}
			IOUtils.close(zip);
			IOUtils.close(fis);
			file.delete();

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		finally{
			IOUtils.close(zip);
			IOUtils.close(fis);
		}
	}

	public static File parseFile(String location){
		if(location.startsWith(FileUtils.CLASS_PATH)){
			return FileUtils.parseFileForClassPath(location);
		}else{
			return new File(location);
		}
	}

	/**
	 * 将路径转换为 java.io.File
	 *
	 * @param location 相对于 ClassPath 的 完整的文件名(路径),可以带上 [classpath:/] 标识,比如<br>
	 *        classpath:/net/coldsky/readme.txt
	 */
	private static File parseFileForClassPath(String location) {
		return FileUtils.parseFileForClassPath(Runtime.getRuntime()., location);
	}

	public static File parseFileForClassPath(Class target, String location) {
		if(location.startsWith(FileUtils.CLASS_PATH)){
			location = location.substring(FileUtils.CLASS_PATH.length(), location.length());
		}
		URL url=target.getResource(location);
		URI uri = null;
		File theFile;
		try{
			uri=url.toURI();
			theFile = new File(uri);
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	
		return theFile;
	}

}
